/*  
 * Copyright (C) 2006 University of South Australia
 *
 * Contributors: 
 *   Andrew Pennington
 *   Ross Smith
 *   Andrew Cunningham
 */
import javax.media.j3d.*;
import java.util.*;

/**
 * The primary hand behaviour class controls the location of the hand geometry, provides the 
 * flying around the world ability and allows us to drag objects at around the world.
 */
public class HandBehaviorPrimary extends HandBehaviorGeneric {

    protected boolean[] button = {false, false, false, false, false}; /** Button states */
	
    public HandBehaviorPrimary(Hand hand, Sensor trackerSensor, ButtonDevice buttonDevice, TransformGroup viewPlatform, TransformGroup worldBranch){
    	super(hand, trackerSensor, buttonDevice, viewPlatform, worldBranch);
    }

    
	public void processStimulus(Enumeration criteria){

       	/* see if the hand's collided with anything. If we've collided
       	 * with a VRObject, pass on a message to the instance */
       	TransformGroup collided = getPickedObjectFromCollision();
		if (collided != null && collided instanceof VRObject)
			((VRObject)collided).collided();
			
       	/*Set the states of the buttons*/
        button[0] = buttonDevice.getButton(ButtonDevice.FORWARD_BUTTON);
       	button[1] = buttonDevice.getButton(ButtonDevice.PRIMARY_PICKING_BUTTON);
       	button[2] = buttonDevice.getButton(ButtonDevice.STRAFE_RIGHT_BUTTON);
       	button[3] = buttonDevice.getButton(ButtonDevice.BACKWARD_BUTTON);
       	button[4] = buttonDevice.getButton(ButtonDevice.STRAFE_LEFT_BUTTON);

       	/* frame movement. Used to keep movement in sync with
       	 * the speed of the computer running it */
       	beginFrameMovement();
       	
       	/*Fly forward*/
		if (button[0])
			forward(FLYING_SPEED);
		/*Fly Backwards*/
		if (button[3])
			forward(-FLYING_SPEED);
		/*Fly left*/
		if (button[4])
			strafe(-FLYING_SPEED);
		/*Fly right*/
		if (button[2])
			strafe(FLYING_SPEED);
		
		endFrameMovement();
		
		/*Set the location of the hand*/
		setHandPosition();
		
		/*Set the look of the hand to represent its actual state*/		
		hand.setTopRightButtonState(button[0]);
		hand.setRightButtonState(button[3]);
		hand.setTopLeftButtonState(button[4]);
		hand.setLeftButtonState(button[2]);
        hand.setTopMiddleButtonState(button[1]);
		
        /* Check if we need to drop an object */
        if (!button[1])
        	testForDrop();
        
        /*Check if we are attempting to grab an object */
    	if (button[1])
    		testForGrab();
    
    	wakeupOn(wakeupConditions);
	}
}
